import axios from "axios";
import { getToken, rmToken, toLogin } from "../user";
import { message, notification } from "antd";
export const baseURL =  location.host.includes('all-round') ? '/api' : `http://${location.hostname}:3000/api`
let res = axios.create({
  baseURL
})
res.interceptors.request.use(res => {
  res.headers['Authorization'] = 'Bearer ' + getToken()
  return res
})
res.interceptors.response.use(res => {
  
  if(res.data.code!=200){
    message.error(res.data.msg)
    return Promise.reject('')
  }
 
  return res.data.data
},(err)=>{
  if(err.message){
      notification.error({
        message:err.message
      })
  }else{
    const code =   err.response.data.code
    if(['credentials_bad_format'].includes(code)){
      rmToken()
      toLogin();
    }
  }
 

// console.log('errerr',err)
})

export default res